package helper

import (
	"mall/service/forum/rpc/forumclient"
	"mall/service/wpgql/api/internal/graph/model"
	"mall/service/wpgql/api/internal/graph/util"

	"github.com/volatiletech/null/v8"
)

func ForumUserIDToGraphQL(v int64) string { ////
	return util.Int64IDToGraphQL(v, util.RpcEntityNames.ForumUser)
}

func ForumUserWithID(id int64) *model.ForumUser { ////
	return &model.ForumUser{
		ID: ForumUserIDToGraphQL(id),
	}
}

func ForumUserWithUintID(id uint) *model.ForumUser { ////
	return &model.ForumUser{
		ID: ForumUserIDToGraphQL(int64(id)),
	}
}

func ForumUserWithIntID(id int) *model.ForumUser { ////
	return ForumUserWithUintID(uint(id))
}

func ForumUserWithNullDotUintID(id null.Uint) *model.ForumUser { ////
	return ForumUserWithUintID(id.Uint)
}

func ForumUserWithNullDotIntID(id null.Int) *model.ForumUser { ////
	return ForumUserWithUintID(uint(id.Int))
}

func ForumAuthorToGraphQL(user *forumclient.ForumAuthor) *model.ForumUser { ////
	if user == nil {
		return nil
	}

	var (
		id           string
		niceName     string
		email        string
		url          string
		ip           string
	)
	if user.Id > 0 {
		id = ForumUserIDToGraphQL(user.Id)
	}
	if user.Name != "" {
		niceName = user.Name
	}
	if user.Email != "" {
		email = user.Email
	}
	if user.Url != "" {
		url = user.Url
	}
	if user.Ip != "" {
		ip = user.Ip
	}

	r := &model.ForumUser{
		ID:           id,
		NiceName:     &niceName,
		Email:        &email,
		URL:          &url,
		IP:           &ip,
	}

	return r
}

func ForumUserToGraphQL(user *forumclient.UserDetailResponse) *model.ForumUser { ////
	if user == nil {
		return nil
	}

	var (
		id           string
		niceName     string
		email        string
		url          string
		ip           string
		displayName  string
		registeredAt string
	)
	if user.Id > 0 {
		id = ForumUserIDToGraphQL(user.Id)
	}
	if user.NiceName != "" {
		niceName = user.NiceName
	}
	if user.Email != "" {
		email = user.Email
	}
	if user.Url != "" {
		url = user.Url
	}
	if user.Ip != "" {
		ip = user.Ip
	}
	if user.DisplayName != "" {
		displayName = user.DisplayName
	}
	if user.RegisteredAt != "" {
		registeredAt = user.RegisteredAt
	}

	r := &model.ForumUser{
		ID:           id,
		NiceName:     &niceName,
		Email:        &email,
		URL:          &url,
		IP:           &ip,
		DisplayName:  &displayName,
		RegisteredAt: &registeredAt,
	}

	// if user.Comments != nil {
	// 	r.Comments = ForumCommentsToGraphQL(user.Comments)
	// }
	// if user.CommentLikes != nil {
	// 	r.CommentLikes = ForumCommentLikesToGraphQL(user.CommentLikes)
	// }
	// if user.Likes != nil {
	// 	r.Likes = ForumLikesToGraphQL(user.Likes)
	// }
	// if user.Posts != nil {
	// 	r.Posts = ForumPostsToGraphQL(user.Posts)
	// }
	// if user.Friendships != nil {
	// 	r.Friendships = ForumFriendshipsToGraphQL(user.Friendships)
	// }

	return r
}

func ForumUsersToGraphQL(am []*forumclient.UserDetailResponse) []*model.ForumUser { ////
	ar := make([]*model.ForumUser, len(am))
	for i, m := range am {
		ar[i] = ForumUserToGraphQL(m)
	}

	return ar
}

func ForumTopicIDToGraphQL(v int64) string {
	return util.Int64IDToGraphQL(v, util.RpcEntityNames.ForumTopic)
}

func ForumTopicWithID(id int64) *model.ForumTopic {
	return &model.ForumTopic{
		ID: ForumTopicIDToGraphQL(id),
	}
}

func ForumTopicWithUintID(id uint) *model.ForumTopic {
	return &model.ForumTopic{
		ID: ForumTopicIDToGraphQL(int64(id)),
	}
}

func ForumTopicWithIntID(id int) *model.ForumTopic {
	return ForumTopicWithUintID(uint(id))
}

func ForumTopicWithNullDotUintID(id null.Uint) *model.ForumTopic {
	return ForumTopicWithUintID(id.Uint)
}

func ForumTopicWithNullDotIntID(id null.Int) *model.ForumTopic {
	return ForumTopicWithUintID(uint(id.Int))
}

func ForumTopicToGraphQL(topic *forumclient.TopicDetailResp) *model.ForumTopic {
	if topic == nil {
		return nil
	}

	var (
		id            string
		title         string
		slug          string
		status        string
		sticky        string
		createdAt     string
		updatedAt     string
		postCount     int
		viewCount     int
	)

	if topic.Id > 0 {
		id = ForumTopicIDToGraphQL(topic.Id)
	}
	if topic.Title != "" {
		title = topic.Title
	}
	if topic.Slug != "" {
		slug = topic.Slug
	}
	// if topic.status != "" {
	// 	status = topic.status
	// }
	// if topic.sticky != "" {
	// 	sticky = topic.sticky
	// }
	if topic.PostCount > 0 {
		postCount = int(topic.PostCount)
	}
	if topic.ViewCount > 0 {
		viewCount = int(topic.ViewCount)
	}
	if topic.CreatedAt != "" {
		createdAt = topic.CreatedAt
	}
	if topic.UpdatedAt != "" {
		updatedAt = topic.UpdatedAt
	}

	r := &model.ForumTopic{
		ID:            id,
		Title:         title,
		Slug:          &slug,
		Status:        &status,
		Sticky:        &sticky,
		CreatedAt:     &createdAt,
		UpdatedAt:     &updatedAt,
		PostCount:     &postCount,
		ViewCount:     &viewCount,
	}

	if util.Int64IsFilled(topic.AuthorId) {
		if topic.Author != nil {
			r.Author = ForumAuthorToGraphQL(topic.Author)
		} else {
			r.Author = ForumUserWithID(topic.AuthorId)
		}
	}

	return r
}

func ForumTopicsToGraphQL(am []*forumclient.TopicDetailResp) []*model.ForumTopic {
	ar := make([]*model.ForumTopic, len(am))
	for i, m := range am {
		ar[i] = ForumTopicToGraphQL(m)
	}

	return ar
}

func ForumPostIDToGraphQL(v int64) string {
	return util.Int64IDToGraphQL(v, util.RpcEntityNames.ForumPost)
}

func ForumPostWithID(id int64) *model.ForumPost {
	return &model.ForumPost{
		ID: ForumPostIDToGraphQL(id),
	}
}

func ForumPostWithUintID(id uint) *model.ForumPost {
	return &model.ForumPost{
		ID: ForumPostIDToGraphQL(int64(id)),
	}
}

func ForumPostWithIntID(id int) *model.ForumPost {
	return ForumPostWithUintID(uint(id))
}

func ForumPostWithNullDotUintID(id null.Uint) *model.ForumPost {
	return ForumPostWithUintID(id.Uint)
}

func ForumPostWithNullDotIntID(id null.Int) *model.ForumPost {
	return ForumPostWithUintID(uint(id.Int))
}

func ForumPostToGraphQL(post *forumclient.PostDetailResp) *model.ForumPost {
	if post == nil {
		return nil
	}

	var (
		id            string
		content       string
		status        string
		commentStatus string
		commentCount  int
		createdAt     string
	)
	if post.Id > 0 {
		id = ForumPostIDToGraphQL(post.Id)
	}
	if post.ContentHtml != "" {
		content = post.ContentHtml
	}
	if post.CommentCount > 0 {
		commentCount = int(post.CommentCount)
	}
	if post.CreatedAt != "" {
		createdAt = post.CreatedAt
	}

	r := &model.ForumPost{
		Content:       content,
		ID:            id,
		Status:        &status,
		CommentStatus: &commentStatus,
		CreatedAt:     &createdAt,
		CommentCount:  &commentCount,
	}

	if util.Int64IsFilled(post.AuthorId) {
		if post.Author != nil {
			r.Author = ForumAuthorToGraphQL(post.Author)
		} else {
			r.Author = ForumUserWithID(post.AuthorId)
		}
	}
	if util.Int64IsFilled(post.TopicId) {
		if post.Topic != nil {
			r.Topic = ForumTopicToGraphQL(post.Topic)
		} else {
			r.Topic = ForumTopicWithID(post.TopicId)
		}
	}

	return r
}

func ForumPostsToGraphQL(am []*forumclient.PostDetailResp) []*model.ForumPost {
	ar := make([]*model.ForumPost, len(am))
	for i, m := range am {
		ar[i] = ForumPostToGraphQL(m)
	}

	return ar
}

func ForumReactionIDToGraphQL(v int64) string {
	return util.Int64IDToGraphQL(v, util.RpcEntityNames.ForumReaction)
}

func ForumReactionWithID(id int64) *model.ForumReaction {
	return &model.ForumReaction{
		ID: ForumReactionIDToGraphQL(id),
	}
}

func ForumReactionWithUintID(id uint) *model.ForumReaction {
	return &model.ForumReaction{
		ID: ForumReactionIDToGraphQL(int64(id)),
	}
}

func ForumReactionWithIntID(id int) *model.ForumReaction {
	return ForumReactionWithUintID(uint(id))
}

func ForumReactionWithNullDotUintID(id null.Uint) *model.ForumReaction {
	return ForumReactionWithUintID(id.Uint)
}

func ForumReactionWithNullDotIntID(id null.Int) *model.ForumReaction {
	return ForumReactionWithUintID(uint(id.Int))
}

func ForumReactionToGraphQL(reaction *forumclient.ReactionDetailResp) *model.ForumReaction {
	if reaction == nil {
		return nil
	}

	var (
		date          string
		id            string
		reactionType  string
	)
	if reaction.CreatedAt != "" {
		date = reaction.CreatedAt
	}
	if reaction.Id > 0 {
		id = ForumReactionIDToGraphQL(reaction.Id)
	}
	if reaction.TypeText != "" {
		reactionType = reaction.TypeText
	}

	r := &model.ForumReaction{
		Date:          &date,
		ID:            id,
		ReactionType:  reactionType,
	}

	if util.Int64IsFilled(reaction.UserId) {
		if reaction.User != nil {
			r.User = ForumAuthorToGraphQL(reaction.User)
		} else {
			r.User = ForumUserWithID(reaction.UserId)
		}
	}
	if util.Int64IsFilled(reaction.TopicId) {
		if reaction.Topic != nil {
			r.Topic = ForumTopicToGraphQL(reaction.Topic)
		} else {
			r.Topic = ForumTopicWithID(reaction.TopicId)
		}
	}
	if util.Int64IsFilled(reaction.PostId) {
		if reaction.Post != nil {
			r.Post = ForumPostToGraphQL(reaction.Post)
		} else {
			r.Post = ForumPostWithID(reaction.PostId)
		}
	}

	return r
}

func ForumReactionsToGraphQL(am []*forumclient.ReactionDetailResp) []*model.ForumReaction {
	ar := make([]*model.ForumReaction, len(am))
	for i, m := range am {
		ar[i] = ForumReactionToGraphQL(m)
	}

	return ar
}
